module sidearm.parser;


struct LALRState
{
	LALRAction[] actions;

	LALRState dup() const
	{
		return LALRState(actions.dup);
	}

	string toString()
	{
		return "%s( %s )".format(typeof(this).stringof, actions);
	}
}

struct LALRAction
{
	enum Type
	{
		Shift  = 1, // target: LALR State Index
		Reduce = 2, // target: Rule Index
		Goto   = 3, // target: LALR State Index
		Accept = 4  // target: unused
	}

	int symbolId;
	Type type;
	int target;

	string toString()
	{
		return
			"%s(%s,%s,%s)".format(
								  typeof(this).stringof, symbolId, fullTypeToString(type), target
								  );
	}

	static string typeToString(Type type)
	{
		switch(type)
		{
			case LALRAction.Type.Shift:
				return "Shift";
			case LALRAction.Type.Reduce:
				return "Reduce";
			case LALRAction.Type.Goto:
				return "Goto";
			case LALRAction.Type.Accept:
				return "Accept";
			default:
				throw new Exception("Unexpected LALRAction.Type #"~to!string(cast(int)type));
		}
	}

	static string fullTypeToString(Type type)
	{
		return typeof(this).stringof ~ "." ~ typeof(type).stringof ~ "." ~ typeToString(type);
	}
}